<?php
/**
 * *
 *  * ============================================================================
 *  * Created by PhpStorm.
 *  * User: Ice
 *  * 邮箱: ice@sbing.vip
 *  * 网址: https://sbing.vip
 *  * Date: 2019/9/20 下午5:19
 *  * ============================================================================.
 */

namespace app\common\service\landlord;

use think\Service;
use app\common\service\RedisMake;
use app\common\service\Socket;

/**
 * 斗地主
 */
class Game extends Index
{

    //定义一套牌
    protected $poker = [];
    //四个花色
    protected $flower = ["C","D","H","S"];     //梅花(Club)为C，方块(Diamond)为D，红心(Hearts)为H，黑桃(Spade)为S。
    //十三个数字
    protected $number = [1,2,3,4,5,6,7,8,9,10,11,12,13];     //梅花(Club)为C，方块(Diamond)为D，红心(Hearts)为H，黑桃(Spade)为S。
    //特殊牌
    protected $special = ["color joker","black joker"];     //梅花(Club)为C，方块(Diamond)为D，红心(Hearts)为H，黑桃(Spade)为S。
    //底牌
    protected $pocket  = [];
    //第一人的牌
    protected $userPoker1  = [];
    //第二人的牌
    protected $userPoker2  = [];
    //第三人的牌
    protected $userPoker3  = [];

    public function __construct($app)
    {
        parent::__construct($app);
        //定义一套牌
        $key = 1;
        foreach($this->number as $val){
            foreach($this->flower as $v){
                $this->poker[] = array(
                    "key" => $key,
                    "flower" => $v,
                    "number" => $val
                );
                $key++;
            }
        }
        foreach($this->special as $val){
            $this->poker[] = array(
                "key" => $key,
                "flower" => "joker",
                "number" => $val
            );
            $key++;
        }
        var_dump($this->poker);die;
    }

    public function start($roomNumber)
    {
        $redis = RedisMake::getRedis();
        $roomData = $redis->get($roomNumber)?:"{}";
        $roomData = json_decode($roomData,true);
        //查询是否够三个人
        if(!$roomData){
            return swoole_error("人数不够，无法开始");
        }
        $flag = true;
        for($n=0;$n<3;$n++){
            if(!isset($roomData[$n]) || !$roomData[$n] || !isset($roomData[$n]["isready"]) || $roomData[$n]["isready"] != 1){
                $flag = false;
                break;
            }
        }
        if(!$flag){
            //return swoole_error("还有玩家未准备，无法开始");
        }
        //洗牌
        shuffle($this->poker);
        //留三张底
        $this->pocket = array_slice($this->poker,51,3);
        //每个人发十七张
        $this->userPoker1 = array_slice($this->poker,0,17);
        $this->userPoker2 = array_slice($this->poker,17,17);
        $this->userPoker3 = array_slice($this->poker,34,17);

        $redis->hSet($roomNumber."_Poker","userPoker1",json_encode($this->userPoker1,JSON_UNESCAPED_UNICODE));
        $redis->hSet($roomNumber."_Poker","userPoker2",json_encode($this->userPoker2,JSON_UNESCAPED_UNICODE));
        $redis->hSet($roomNumber."_Poker","userPoker3",json_encode($this->userPoker3,JSON_UNESCAPED_UNICODE));
        $redis->hSet($roomNumber."_Poker","pocket",json_encode($this->pocket,JSON_UNESCAPED_UNICODE));
        //通知每个人
        foreach($roomData as $v){
            $data = [];
            $next = 0;
            $last = 0;
            switch ($v['sort']){
                case 1:
                    $data = $this->userPoker1;
                    $next = count($this->userPoker2);
                    $last = count($this->userPoker3);
                    break;
                case 2:
                    $data = $this->userPoker2;
                    $next = count($this->userPoker3);
                    $last = count($this->userPoker1);
                    break;
                case 3:
                    $data = $this->userPoker3;
                    $next = count($this->userPoker1);
                    $last = count($this->userPoker2);
                    break;
                default:
                    break;
            }
            //除了自己，其他人只返回张数
            $data = array(
                "my_poker" => $data,
                "next" => $next,
                "last" => $last
            );
            $message = swoole_success("游戏开始了",$data,"game_start");
            Socket::sendMsg($message,$v['fd']);
        }
        return swoole_success("游戏开始！");
    }


    /**
     * 整理扑克
     */
    private function sortPoker($poker){
        if(!is_array($poker)){
            return array();
        }
        $new = array();
        foreach ($poker as $k=>$v){



        }
    }

}
